/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package infoserver;

/**
 *
 * @author Erik
 */

import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Log {
    
    private File file;
    private String path;
    private Writer writer = null;
    
    public Log(String path){
        this.path = path;
    }
    
    public void createLog(){
        file = new File(path, getfileName());
            if(!file.exists()){
                try{
                    file.createNewFile();
                }
                catch(IOException e){
                    System.out.println(e.getMessage());
                }
            }
            else{
                System.out.println("File already excists");
            }
    }
    
    public String getDate(){
        DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
        Date date = new Date();
        return dateFormat.format(date);
    }
    
    public String getHour(){
        DateFormat dateFormat = new SimpleDateFormat("HH");
        Date date = new Date();
        return dateFormat.format(date);
    }
    
    public String getMinute(){
        DateFormat dateFormat = new SimpleDateFormat("mm");
        Date date = new Date();
        return dateFormat.format(date);
    }
    
    public String getSecond(){
        DateFormat dateFormat = new SimpleDateFormat("ss");
        Date date = new Date();
        return dateFormat.format(date);
    }
    
    public String getfileName(){ //mag geen / bevatten dit zorgt voor errors, ook geen tekens die niet ondersteurnt worden door windows
        return "Logfile " + getDate() + " " + getHour() + "u" + getMinute() + "m" + getSecond() + "s.log";
    }
    
    public static File lastFileModified(String dir) {
        File fl = new File(dir);
        File[] files = fl.listFiles(new FileFilter() {                  
                @Override
                public boolean accept(File file) {
                        return file.isFile();
                }
        });
        long lastMod = Long.MIN_VALUE;
        File choise = null;
        for (File file : files) {
                if (file.lastModified() > lastMod) {
                        choise = file;
                        lastMod = file.lastModified();
                }
        }
        return choise;
    }
    
    public void writeLog(String s){
        file = lastFileModified(path);
        try{
            BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
            bw.write(getHour() + ":" + getMinute() + " " + s);
            bw.newLine();
            bw.close();
        }
        catch(FileNotFoundException e){
            System.out.println(e.getMessage());
        }
        catch(IOException e){
            System.out.println(e.getMessage());
        }
        finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } 
            catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }
}
